import rclpy
from rclpy.node import Node
from rclpy.action import ActionServer
from rclpy.action.server import ServerGoalHandle
import time
from base_interface_demo01.action import Progress
class ProgressActionServerNode(Node):
    def __init__(self):
        super().__init__('progress_action_server_node_py')
        self.get_logger().info('ProgressActionServerNode')
        self.server=ActionServer(self,Progress,"get_sum",self.execute_callback)
    def execute_callback(self,goal_handler:ServerGoalHandle):
        # 生成连续返回和响应
        num=goal_handler.request.num
        sum=0
        for i in range(1,num+1):
            sum+=i
            feedback=Progress.Feedback()
            feedback.progress=i/num
            goal_handler.publish_feedback(feedback)
            self.get_logger().info('连续反馈%.2f'%feedback.progress)
            time.sleep(1.0)
        goal_handler.succeed()
        result=Progress.Result()
        result.sum=sum
        self.get_logger().info('最终结果%d'%sum)  
        return result
def main():
    rclpy.init()
    rclpy.spin(ProgressActionServerNode())
    rclpy.shutdown()


if __name__ == '__main__':
    main()